package com.ruoyi.teaching.service.impl;

import java.util.*;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.teaching.domain.InterviewMasteryDetail;
import com.ruoyi.teaching.domain.vo.InsertInterviewMasery;
import com.ruoyi.teaching.domain.vo.InterviewMaseryList;
import com.ruoyi.teaching.domain.vo.SelectInterviewMasery;
import com.ruoyi.teaching.mapper.InterviewMasteryDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.InterviewMasteryMapper;
import com.ruoyi.teaching.domain.InterviewMastery;
import com.ruoyi.teaching.service.IInterviewMasteryService;

/**
 * 面试掌握情况记录Service业务层处理
 *
 * @author sunshine
 * @date 2024-01-25
 */
@Service
public class InterviewMasteryServiceImpl implements IInterviewMasteryService
{
    @Autowired
    private InterviewMasteryMapper interviewMasteryMapper;
    @Autowired
    private InterviewMasteryDetailMapper interviewMasteryDetailMapper;

    /**
     * 查询面试掌握情况记录
     *
     * @param id 面试掌握情况记录主键
     * @return 面试掌握情况记录
     */
    @Override
    public InterviewMastery selectInterviewMasteryById(Long id)
    {
        return interviewMasteryMapper.selectInterviewMasteryById(id);
    }

    /**
     * 查询面试掌握情况记录列表
     *
     * @param interviewMastery 面试掌握情况记录
     * @return 面试掌握情况记录
     */
    @Override
    public List<InterviewMastery> selectInterviewMasteryList(InterviewMastery interviewMastery)
    {
        return interviewMasteryMapper.selectInterviewMasteryList(interviewMastery);
    }

    /**
     * 新增面试掌握情况记录
     *
     * @param interviewMastery 面试掌握情况记录
     * @return 结果
     */
    @Override
    public int insertInterviewMastery(InsertInterviewMasery interviewMastery)
    {
//        添加面试掌握记录
        interviewMastery.setCreateBy(SecurityUtils.getUsername());
        interviewMastery.setCommunicationScore(CommunicationMasterySore(interviewMastery));
        interviewMastery.setCreateTime(new Date());

        int i = interviewMasteryMapper.insertInterviewMastery(interviewMastery);
        if (i == 0){
            return 0;
        }
        Long id = interviewMastery.getId();
        Long userId = interviewMastery.getUserId();
        List<String> selectedInterviewList = interviewMastery.getSelectedInterviewList();
        if (CollectionUtils.isEmpty(selectedInterviewList)){
            throw new ServiceException("没有选择数据");
        }
//        数据转换
        Map<Long, Set<String>> hashMap = new HashMap<>(selectedInterviewList.size());
        for(String str:selectedInterviewList){
            String[] split = str.split(",");
           Long questionId = Long.valueOf(split[0]);
            String degree = split[1];
//            判断是都存在这个set对象
            if (hashMap.containsKey(questionId)){
                Set<String> strings = hashMap.get(questionId);
                strings.add(degree);
            }else {
                Set<String> set = new HashSet<>();
                set.add(degree);
                hashMap.put(questionId,set);
            }
        }
        int total = 0;
        for (Map.Entry<Long,Set<String>> entry : hashMap.entrySet()){
            Long questionId = entry.getKey();
            Set<String> set = entry.getValue();
            Integer score = InterviewScore(set);
            total+=score;
            String join = String.join(",", set);
            interviewMasteryDetailMapper.insertInterviewMasteryDetail(new InterviewMasteryDetail(questionId,id,join,score,userId,interviewMastery.getCreateTime()));
        }
        int avgScore = total/hashMap.size();
        interviewMastery.setAvgScore(avgScore);
        interviewMasteryMapper.updateInterviewMastery(interviewMastery);
        return 1;
    }

    private Integer InterviewScore(Set<String> set) {
        if (CollectionUtils.isEmpty(set)){
            return 0;
        }
        return set.stream().mapToInt(str ->{
            if ("1".equals(str)){
                return 0;
            }else if ("2".equals(str)){
                return 1;
            }else {
                return 2;
            }
        }).sum();
    }

    private Integer CommunicationMasterySore(InsertInterviewMasery interviewMastery) {
        if (StringUtils.isEmpty(interviewMastery.getCommunicationMastery())){
            return 0;
        }
        return interviewMastery.getCommunicationMastery().split(",").length;
    }

    /**
     * 修改面试掌握情况记录
     *
     * @param interviewMastery 面试掌握情况记录
     * @return 结果
     */
    @Override
    public int updateInterviewMastery(InterviewMastery interviewMastery)
    {
        interviewMastery.setUpdateTime(DateUtils.getNowDate());
        return interviewMasteryMapper.updateInterviewMastery(interviewMastery);
    }

    /**
     * 批量删除面试掌握情况记录
     *
     * @param ids 需要删除的面试掌握情况记录主键
     * @return 结果
     */
    @Override
    public int deleteInterviewMasteryByIds(Long[] ids)
    {
        return interviewMasteryMapper.deleteInterviewMasteryByIds(ids);
    }

    /**
     * 删除面试掌握情况记录信息
     *
     * @param id 面试掌握情况记录主键
     * @return 结果
     */
    @Override
    public int deleteInterviewMasteryById(Long id)
    {
        return interviewMasteryMapper.deleteInterviewMasteryById(id);
    }


}
